package com.avic.rong.search.adapter.homePageAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.transform.activity.DetailTransformActivity;
import com.avic.rong.transform.bean.SupplyAndDemandBean;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.GlideUtils;
import com.avic.rong.wedgit.customView.RoundImageView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 * 收缩版adapter
 */
public class TransformAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<SupplyAndDemandBean> beanList = new ArrayList<>();
    private int type = 0;

    public void setBeanList(List<SupplyAndDemandBean> beanList) {
        this.beanList.addAll(beanList);
        notifyDataSetChanged();
    }

    public List<SupplyAndDemandBean> getBeanList() {
        return beanList;
    }

    public TransformAdapter(Context context, int type) {
        this.context = context;
        this.type = type;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int groupType) {

        View view = null;
        if (groupType == 0) {
            view = LayoutInflater.from(context).inflate(R.layout.view_transform_demand, viewGroup, false);
            return new DemandViewHolder(view);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.view_transform_supply, viewGroup, false);
            return new SupplyViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int i) {


        SupplyAndDemandBean bean = beanList.get(i);

        if (holder instanceof DemandViewHolder) {//需求


            DemandViewHolder demandViewHolder = (DemandViewHolder) holder;
            demandViewHolder.tv_name.setText(beanList.get(i).getTitle());
            if (bean.getIsCompany() == 1){
                demandViewHolder.tv_demand_company.setText("企业");
                demandViewHolder.tv_demand_company.setSelected(true);
            }else{
                demandViewHolder.tv_demand_company.setSelected(false);
                demandViewHolder.tv_demand_company.setText("个人");
            }
            demandViewHolder.tv_demand_publish_time.setText(bean.getPublishTime());
            demandViewHolder.tv_demand_last_time.setText(bean.getLastTime());
//            demandViewHolder.tv_demand_sub_type.setText(bean.get);

            holder.itemView.setOnClickListener(new MyOnClickListener(bean.getDemandId()));

        } else {
            SupplyViewHolder supplyViewHolder = (SupplyViewHolder) holder;
            supplyViewHolder.tv_name.setText(beanList.get(i).getTitle());
            supplyViewHolder.tv_supply_company_name.setText("公司名称： " + bean.getCompanyName());
            supplyViewHolder.tv_supply_province.setText("所在地： " + bean.getProvinceName());
            if (bean.getPrice() > 0){
                DecimalFormat df = new DecimalFormat("###0.00");
                String format = df.format(bean.getPrice());
                supplyViewHolder.tv_supply_price.setText(Html.fromHtml("参考价格: <font color='red'>" + format + "元</font>"));
            }else{
                supplyViewHolder.tv_supply_price.setText(Html.fromHtml("参考价格: <font color='red'>面议</font>"));
            }

            GlideUtils.loaderHanldeRoundImage(context, bean.getFiles(), supplyViewHolder.img_supply_logo, R.mipmap.online_default, DensityUtils.dp2px(context, 6));

            holder.itemView.setOnClickListener(new MyOnClickListener(bean.getSupplyId()));
        }

    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }


    @Override
    public int getItemViewType(int position) {
        return type;
    }


    class DemandViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_demand_company)
        TextView tv_demand_company;
        @BindView(R.id.tv_demand_publish_time)
        TextView tv_demand_publish_time;//发布时间
        @BindView(R.id.tv_demand_last_time)
        TextView tv_demand_last_time;
        @BindView(R.id.tv_demand_sub_type)
        TextView tv_demand_sub_type;


        public DemandViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    class SupplyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.img_supply_logo)
        RoundImageView img_supply_logo;
        @BindView(R.id.tv_supply_company_name)
        TextView tv_supply_company_name;
        @BindView(R.id.tv_supply_province)
        TextView tv_supply_province;
        @BindView(R.id.tv_supply_price)
        TextView tv_supply_price;


        public SupplyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    class MyOnClickListener implements View.OnClickListener{

        private int beanId;

        public MyOnClickListener(int beanId) {
            this.beanId = beanId;
        }

        @Override
        public void onClick(View v) {
            context.startActivity(DetailTransformActivity.getIntent(context,beanId));
        }
    }


}
